package com.naihe.anderson.sms.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.entity.NhsmsSession;
import com.naihe.anderson.sms.manager.ContactMgr;

public class CHeckSessionAdapter extends BaseAdapter {

	private Context mContext;
	private List<NhsmsSession> mObjects;
	private ContactMgr contactMgr;

	private Map<NhsmsSession, Boolean> checkIfMap = new HashMap<NhsmsSession, Boolean>();
	
	public CHeckSessionAdapter(Context context, List<NhsmsSession> objects) {
		this.mContext = context;
		this.mObjects = objects;
		contactMgr = ((NhSmsApp) context.getApplicationContext()).getContactMgr();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			view = inflater.inflate(R.layout.session_check_item, null);
		} else {
			view = convertView;
		}
		NhsmsSession entityObj = mObjects.get(position);

		CheckBox widget_checkbox = (CheckBox) view.findViewById(R.id.widget_checkbox);
		Boolean ifcheck = checkIfMap.get(entityObj);
        if(ifcheck==null){
            checkIfMap.put(entityObj, false);
            ifcheck = false;
        }
        widget_checkbox.setChecked(ifcheck);
		ImageView contactImage = (ImageView) view.findViewById(R.id.contactImage);
		TextView smsNum = (TextView) view.findViewById(R.id.sms_num);
		TextView smsTime = (TextView) view.findViewById(R.id.sms_time);
		TextView smsContent = (TextView) view.findViewById(R.id.sms_content);
		TextView smsName = (TextView) view.findViewById(R.id.sms_name);

		ContactUser contactUser = contactMgr.getContactUserById(contactMgr.getContactIdByPhoneNum(entityObj.getAddress()));
		if (contactUser != null && contactUser.getPhotoId() > 0) {
			contactImage.setBackgroundDrawable(new BitmapDrawable(contactMgr.getContactHeadImgByPhotoId(contactUser.getPhotoId())));
		} else {
			contactImage.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.default_head));
		}

		if (contactUser != null) {
			smsName.setText(contactUser.getName());
		} else {
			smsName.setText(entityObj.getAddress());
		}
		smsNum.setText("(" + entityObj.getNumsize() + ")");
		smsTime.setText(getDataFormat(entityObj.getDate()));
		if(TextUtils.isEmpty(entityObj.getCipher())){
			smsContent.setText(entityObj.getBody());
		} else {
			smsContent.setText("『加密』");
		}

		return view;
	}

	private String getDataFormat(long time) {
		String formatTime = "";
		Date date = new Date(time);
		try {
			Date nowDate = new Date();
			if (date.getYear() == nowDate.getYear()
					&& date.getMonth() == nowDate.getMonth()
					&& date.getDay() == nowDate.getDay()) {
				formatTime = mContext.getResources().getString(R.string.today)
						+ new SimpleDateFormat("HH:mm").format(date);
			} else if (date.getYear() == nowDate.getYear()
					&& date.getMonth() == nowDate.getMonth()
					&& date.getDay() == (nowDate.getDay() - 1)) {
				formatTime = mContext.getResources().getString(
						R.string.yesterday)
						+ new SimpleDateFormat("HH:mm").format(date);
			} else if (date.getYear() == nowDate.getYear()) {
				formatTime = new SimpleDateFormat("MM-dd").format(date);
			} else {
				formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.format(date);
			}
		} catch (Exception e) {
			formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(date);
			;
		}
		return formatTime;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mObjects == null)
			return 0;
		return mObjects.size();
	}

	@Override
	public NhsmsSession getItem(int position) {
		// TODO Auto-generated method stub
		if (mObjects == null)
			return null;
		return mObjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public Map<NhsmsSession, Boolean> getCheckIfMap() {
        return checkIfMap;
    }

    public void setCheckIfMap(NhsmsSession entityObj,boolean values){
        checkIfMap.put(entityObj, values);
    }
    
    public void clearCheckIfMap(){
    	checkIfMap.clear();
    }
}
